﻿using System;
using System.Collections.Generic;
using Assist;
using Autodesk.Revit.DB;
using YJKRevitKernel.ElementSearchUtility.LinkDocumentSuite;
using YJKPatternSign.StairsRoomSection.DataDefine;
using YJKPatternSign.StairsRoomSection.Kit;

namespace YJKPatternSign.StairsRoomSection.ElementPacket
{
	public class WallObject : ComLayerObject
	{
		public WallObject(BaseLinkDocumentResultBean LinkElem, SectionViewData secviewData = null) : base(LinkElem, secviewData)
		{
			this.m_objectType = ObjectType.kWallType;
		}

		public override List<ElementObject> DividChild()
		{
			base.DividChild();
			foreach (ElementObject elementObject in base.LstChildObj)
			{
				elementObject.m_objectType = ObjectType.kWallLayerType;
				(elementObject as LayerObject).m_MaxLayerIndex = base.MaxLayerIndex;
				(elementObject as LayerObject).m_MinLayerIndex = base.MinLayerIndex;
				(elementObject as LayerObject).m_parentObj = this;
			}
			return base.LstChildObj;
		}

		public override Plane GetLastLayerCutPlane(int index, Line cutLine)
		{
			Wall wall = base.SelfElem.UseElement as Wall;
			this.GetAngelToCutLine(cutLine);
			XYZ xyz = XYZ.Zero;
			IntersectionResult inPoint = this.GetInPoint(cutLine);
			if (inPoint == null)
			{
				xyz = (wall.Location as LocationCurve).Curve.Evaluate(0.5, true);
			}
			else
			{
				xyz = inPoint.XYZPoint;
			}
			bool flag = this.SureDir(cutLine);
			double num = 0.0;
			bool flag2 = false;
			foreach (ElementObject elementObject in base.LstChildObj)
			{
				LayerObject layerObject = elementObject as LayerObject;
				if (layerObject.m_IndexPos == index)
				{
					num += layerObject.m_dWidth;
					flag2 = true;
					break;
				}
				num += layerObject.m_dWidth;
			}
			if (!flag2)
			{
				return null;
			}
			XYZ norDir = this.GetNorDir(cutLine);
			this.GetAngelToCutLine(cutLine);
			XYZ xyz2 = norDir;
			if (!flag)
			{
				xyz2 = -xyz2;
			}
			xyz += (wall.Width / 2.0 - num) * xyz2;
			return RevitVersionFuncs.CreatePlanByNormalAndOrigin(xyz2, xyz);
		}

		private bool SureDir(Line cutLine)
		{
			Wall wall = base.SelfElem.UseElement as Wall;
			Curve curve = (wall.Location as LocationCurve).Curve;
			XYZ orientation = wall.Orientation;
			XYZ xyz = new XYZ(0.0, 0.0, 0.0);
			if (!(curve is Arc))
			{
				xyz = this.GetNorDir(cutLine);
			}
			else
			{
				double endParameter = curve.GetEndParameter(0);
				xyz = curve.ComputeDerivatives(endParameter, false).BasisX;
				xyz = Transform.CreateRotation(new XYZ(0.0, 0.0, 1.0), Math.PI*.5).OfVector(xyz).Normalize();
			}
			return orientation.AngleTo(xyz) < 1E-06;
		}

		private double GetAngelToCutLine(Line cutLine)
		{
			Curve curve = ((base.SelfElem.UseElement as Wall).Location as LocationCurve).Curve;
			XYZ norDir = this.GetNorDir(cutLine);
			return (cutLine.GetEndPoint(1) - cutLine.GetEndPoint(0)).AngleTo(norDir);
		}

		private XYZ GetNorDir(Line cutLine)
		{
			XYZ xyz = new XYZ(0.0, 0.0, 0.0);
			Curve curve = ((base.SelfElem.UseElement as Wall).Location as LocationCurve).Curve;
			if (curve is Arc)
			{
				IntersectionResult inPoint = this.GetInPoint(cutLine);
				double num = 0.0;
				if (inPoint != null)
				{
					num = curve.ComputeNormalizedParameter(inPoint.Parameter);
				}
				xyz = curve.ComputeDerivatives(num, true).BasisX;
				xyz = Transform.CreateRotation(new XYZ(0.0, 0.0, 1.0), Math.PI*.5).OfVector(xyz).Normalize();
			}
			else
			{
				XYZ endPoint = curve.GetEndPoint(0);
				XYZ endPoint2 = curve.GetEndPoint(1);
				xyz = Transform.CreateRotation(new XYZ(0.0, 0.0, 1.0), Math.PI*.5).OfVector(endPoint2 - endPoint).Normalize();
			}
			return xyz;
		}

		private IntersectionResult GetInPoint(Line cutLine)
		{
			new XYZ(0.0, 0.0, 0.0);
			Curve curve = ((base.SelfElem.UseElement as Wall).Location as LocationCurve).Curve;
			double z = curve.GetEndPoint(0).Z;
			IntersectionResultArray intersectionResultArray = null;
			XYZ endPoint = cutLine.GetEndPoint(0);
			XYZ endPoint2 = cutLine.GetEndPoint(1);
			Line line = Line.CreateBound(new XYZ(endPoint.X, endPoint.Y, z), new XYZ(endPoint2.X, endPoint2.Y, z));
			curve.Intersect(line, out intersectionResultArray);
			if (intersectionResultArray == null)
			{
				return null;
			}
			IntersectionResult result = null;
			if (intersectionResultArray.Size >= 1)
			{
				result = curve.Project(intersectionResultArray.get_Item(0).XYZPoint);
			}
			return result;
		}
	}
}
